//
//  HomePageHeaderDetail.m
//  Z0AKER
//
//  Created by dllo on 15/9/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HomePageHeaderDetail.h"


@interface HomePageHeaderDetail ()

@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) UIScrollView *bigScrollView;
@property (nonatomic, retain) MBProgressHUD *mb;

@end

@implementation HomePageHeaderDetail

-(void)dealloc
{
    
    [_dataArray release];
    [_bigScrollView release];
    [_mb release];
    [_model release];
    
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.dataArray = [NSMutableArray array];
        
    }
    
    return self;
    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    // 把加载数据的风火轮加到window上
    UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
    self.mb = [[MBProgressHUD alloc] initWithWindow:window];
    _mb.labelText = @"正在加载";
    
    // 加一个线程, 8秒后数据未加载出来, 给出提示
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        if (_mb != nil) {
            [_mb removeFromSuperview];

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"数据加载失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert  show];
            [alert release];
            
        }
    });
    [window addSubview:_mb];
    [_mb show:YES];
    [_mb release];
    [self myConnection];
    
}

#pragma mark - 数据解析

-(void)myConnection
{
    
    NSArray *array = [self.model.photosetID componentsSeparatedByString:@"|"];
    NSString *str = array.lastObject;
    
    [MyAFNetworking GetWithURL:[NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/0096/%@.json", str] dic:nil data:^(id responsder) {
        
        NSArray *myArray = responsder[@"photos"];
        [self.dataArray addObjectsFromArray:myArray];
        
        [self createMyView];
        
         _mb = nil;
        [_mb hide:YES];
      
    }];

}

#pragma mark - 自定义View放头条的照片和标题

- (void)createMyView
{
    
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
    _bigScrollView.bounces = NO;
    _bigScrollView.pagingEnabled = YES;
    [self.view addSubview:_bigScrollView];
    self.bigScrollView.contentSize = CGSizeMake(self.view.frame.size.width * [_dataArray count], 0);
    
    CGFloat offX = 0;
    for (int i = 0; i < [_dataArray count]; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(offX, 64, self.view.frame.size.width, _bigScrollView.frame.size.height)];
        NSDictionary *dic = _dataArray[i];
        NSString *imageURl = dic[@"imgurl"];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageURl]];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(offX, 150, self.view.frame.size.width, 400)];
        label.textColor = [UIColor whiteColor];
        label.numberOfLines = 20;
        NSString *note = dic[@"note"];
        label.text = note;
        
        [_bigScrollView addSubview:imageView];
        [imageView release];
        [_bigScrollView addSubview:label];
        [label release];
        
        offX += self.view.frame.size.width;
        [_bigScrollView release];
        
    }
}

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
   
}


@end
